package com.rzj.book.controller;

import com.alibaba.fastjson.JSON;
import com.rzj.book.bo.EmpBO;
import com.rzj.book.en.Emp;
import com.rzj.book.en.Role;
import com.rzj.book.service.IEmpService;
import com.rzj.book.service.IMenuService;
import com.rzj.book.service.IRoleService;
import com.rzj.book.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:
 * @author: rzj
 * @time: 2020/6/8 22:10
 */
@RestController
@RequestMapping("/emp")
public class EmpController extends BaseController {

    @Autowired
    private IEmpService empService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @RequestMapping("/getEmpList")
    public void getEmpList(Integer page, Integer rows, EmpBO empBO) {
        PageResult<Emp> list = empService.getEmpBOList(page, rows, empBO);
        write(JSON.toJSONString(list));
    }

    @RequestMapping("getEmpById")
    public String getEmpById(Integer id) {
        Emp emp = empService.getEmpById(id);
        write(JSON.toJSONString(emp));
        return null;
    }

    @RequestMapping("getRolesByEmpId")
    public String getRolesByEmpId(Integer empId) {
        List<Role> roleList = roleService.getRoleListByEmpId(empId);
        write(JSON.toJSONString(roleList));
        return null;
    }

    @RequestMapping("delEmpById")
    public String deleteEmpById(Integer id) {
        try {
            empService.deleteEmpById(id);
            ajaxReturn(true, "删除成功");
        } catch (Exception e) {
            ajaxReturn(false, "删除失败");
            e.printStackTrace();
        }

        return null;
    }

}
